package com.code.aspect;

import android.Manifest;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.code.aspectj.AppInterceptor;
import com.code.aspectj.annotation.MainThread;
import com.code.aspectj.annotation.Permission;
import com.code.aspectj.annotation.Safe;
import com.code.aspectj.annotation.SingleClick;
import com.code.aspectj.util.P;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Created by RLZ
 * on 2021/12/7
 */
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private int mCount = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.t1).setOnClickListener(v -> per());
        findViewById(R.id.t2).setOnClickListener(this::click);
        findViewById(R.id.t3).setOnClickListener(this::safe);
        AppInterceptor.setOnPermissionDeniedListener(permissionsDenied -> P.d(TAG, "拒绝了权限"));
        AppInterceptor.setSafeHandler((flag, t) -> {
            if (TextUtils.equals(flag, "123")) {
                return 100;
            } else {
                Toast.makeText(this, flag, Toast.LENGTH_SHORT).show();
            }
            return null;
        });
    }


    private void safe(View v) {
        int number = getNumber();
        //Toast.makeText(this, number + "", Toast.LENGTH_SHORT).show();
        ((TextView) findViewById(R.id.t1)).setText(number + " ");
    }

    @Safe()
    private int getNumber() {
        return 100 / 0;
    }

    @MainThread
    private void show() {
        Toast.makeText(this, "当前子线程运行  " + (Looper.getMainLooper() == Looper.myLooper()), Toast.LENGTH_SHORT).show();
        new Dialog(this).show();
    }

    /**
     * 次方法必须传递需要检验的View
     */
    @SingleClick(value = 2000)
    private void click(View v) {
        mCount++;
        P.d(TAG, "点击了一次 " + mCount);
    }

    /**
     * 权限申请
     */
    @Permission(value = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE})
    public void per() {
        P.d(TAG, "已有权限");
    }


}